package com.atzxh.java;

/**
 * @author 小红同学
 * @creat 2021-11-09-18:27
 * @description      用同步方法来解决继承Thread类的线程安全问题
 */
class Window6 extends Thread{

    private static int ticket = 100;

    public void run() {
        while (true){
            show();
        }
    }

    private static synchronized void show(){    //此时同步监视器: Window6.class
        if(ticket > 0){
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":"+ticket);
            ticket--;
        }
    }
}


public class WindowTest5 {
    public static void main(String[] args) {
        Window6 w1 = new Window6();
        Window6 w2 = new Window6();
        Window6 w3 = new Window6();

        w1.setName("窗口1");
        w2.setName("窗口2");
        w3.setName("窗口3");

        w1.start();
        w2.start();
        w3.start();
    }
}
